package com.company.cloud.behavior.modules.app.controller;

import java.util.function.Function;

import com.company.cloud.behavior.modules.app.controller.req.FollowIsUserParam;
import com.company.cloud.common.asserts.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.behavior.modules.app.controller.req.FollowQuery4PageParam;
import com.company.cloud.behavior.modules.app.controller.req.FollowSaveParam;
import com.company.cloud.behavior.modules.base.entity.UserFollowInfo;
import com.company.cloud.behavior.modules.base.service.UserFollowInfoService;
import com.company.cloud.behavior.modules.cache.models.BehaviorSearchModel;
import com.company.cloud.behavior.modules.cache.service.BehaviorCacheService;
import com.company.cloud.behavior.modules.cache.utils.BehaviorConvertUtils;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseConstant.YNEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.log.annotation.SysLog;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/**
 * 关注
 */
@Login
@RestController
@RequestMapping("/app/behavior/follow")
@Api(tags="App_Behavior_关注服务")
public class FollowController {

	@Autowired
	private UserFollowInfoService userFollowInfoService;

	@Autowired
	private BehaviorCacheService behaviorCacheService;

	@PostMapping("/query")
	@ApiOperation(value="关注列表查询", response=BaseResult.class)
	public BaseResult query(@RequestBody FollowQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<UserFollowInfo> page = userFollowInfoService.page(new Page<>(params.getPage(), params.getRows()),
				new QueryWrapper<UserFollowInfo>()
						.eq("user_id", userInfo.getId())
						.eq(CheckUtils.hasText(params.getObjType()), "obj_type", params.getObjType())
						.eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
						.orderByDesc("cdt")
		);

		IPage<BehaviorSearchModel> data = page.convert(new Function<UserFollowInfo, BehaviorSearchModel>() {
			@Override
			public BehaviorSearchModel apply(UserFollowInfo entity) {
				BehaviorSearchModel model = BehaviorConvertUtils.getBehaviorSearchModel(entity);
				// 获取关注对象 TODO
				return model;
			}
		});

		return BaseResult.ok();
	}

	@PostMapping("/save")
	@ApiOperation(value="新增",response = String.class)
	public BaseResult save(@RequestBody FollowSaveParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		UserFollowInfo favorite= userFollowInfoService.getOne(new QueryWrapper<UserFollowInfo>()
				.eq("company_no", userInfo.getCompanyNo())
				.eq("user_id", userInfo.getId())
				.eq("obj_type", params.getObjType())
				.eq("obj_no", params.getObjNo())
				.eq("rows_state", EnabledEnum.ENABLED.getCode())
				);
		if(favorite!=null) {
			return BaseResult.ok();
		}
		UserFollowInfo entity=new UserFollowInfo();
		entity.setCompanyNo(userInfo.getCompanyNo());
		entity.setUserId(userInfo.getId());
		entity.setObjType(params.getObjType());
		entity.setObjNo(params.getObjNo());
		entity.setObjIsUpdown(YNEnum.YES.getCode());
		entity.addCreateParam(userInfo);
		userFollowInfoService.save(entity);
		behaviorCacheService.updateFollowCount(params.getObjType(), params.getObjNo(), 1);
		return BaseResult.ok(entity.getId());
	}
	
	@GetMapping("/delete")
	@ApiOperation(value="删除")
	public BaseResult delete(@RequestParam("id")Integer id) {
		UserFollowInfo entity=userFollowInfoService.getById(id);
		if(null == entity || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		entity.setRowsState(EnabledEnum.DISABLED.getCode());
		entity.addEditParam(userInfo);
		userFollowInfoService.updateById(entity);
		behaviorCacheService.updateFollowCount(entity.getObjType(), entity.getObjNo(), -1);
		return BaseResult.ok();
	}



	@PostMapping("/isFollow" )
	@ApiOperation(value="当前用户是否关注",response = String.class)
	public BaseResult isFollow(@RequestBody FollowIsUserParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		UserFollowInfo favorite= userFollowInfoService.getOne(new QueryWrapper<UserFollowInfo>()
				.eq("user_id", userInfo.getId())
				.eq("obj_type", params.getObjType())
				.eq("obj_no", params.getObjNo())
				.eq("rows_state", EnabledEnum.ENABLED.getCode())
		);
		if(favorite!=null) {
			return BaseResult.ok(YNEnum.YES.getCode());
		}
		return BaseResult.ok(YNEnum.NO.getCode());
	}


}
